import { obj_str, is_empty, uDb, cline } from 'sui';

const get_mt = (v) => {
    const ret = {
        "G": "钢球",
        "T": "陶瓷球",
        "C": "保持架",
        "S": "密封圈",
        "pick": "领料单",
        "back": "退料单",
        "order": "入库单",
    };

    return ret[v];
};

const get_side = (v) => {
    const ret = {
        "hand": "人工装配线",
        "auto": "自动装配线",
    };

    return ret[v];
};

const get_tp = (v) => {
    const ret = {
        "G": "stell",
        "T": "cera",
        "C": "cage",
        "S": "seal",
    };

    return ret[v];
};

const load_supply = (cur, setMain) => {
    const mod = get_tp(cur);
    if (!mod) {
        setMain(null);
        return;
    }

    uDb(null, "file/read/group", { line: cline() },
        r => {
            const group = r[mod];
            if (!group) {
                setMain(null);
                return;
            }

            const user = group.user;
            if (is_empty(user)) {
                setMain(null);
                return;
            }

            uDb(null, "file/read/supply", {line: cline()},
                t => {
                    const ret = [];

                    user.forEach(u => {
                        const tmp = t[u];
                        if (!tmp) {
                            return;
                        }

                        ret.push({id: tmp.sn, des: tmp.name});
                    });

                    setMain(ret);
                }, e => setMain(null));

        }, e => setMain(null));
};

const load_pick = (hDone, setMain, mod="pick") => {
    uDb(hDone, `${mod}/list`, { line: cline() },
        r => setMain(r),
        e => setMain(null));
};

const print_value = (v) => {
    if (v > 0) {
        return `+${v}`
    }

    return `${v}`;
};

const load_stuff = (setMain) => {
    uDb(null, "file/list/stuff", { line: cline() },
        r => {
            const ret = [];

            r.forEach(d => {
                ret.push({
                    id: d.sn,
                    des: d.name,
                })
            });

            setMain(ret);
        },
        e => setMain(null));
};

const qr_cmd = (qr) => {
    const tmp = qr.split("#");
    if (tmp.length !== 7) {
        return null;
    }

    const mt = tmp[0];
    const sn = tmp[2];
    const ext = tmp[3];

    const ext_tmp = ext.split("*");

    const cmd = {
        sn: sn,
        qr: qr,
        tmpl: mt.toLowerCase(),
    };

    const getV = (p) => {
        const t = ext_tmp[p];

        if (!t) {
            return "";
        }

        return t;
    };

    if (mt === "G" || mt === "T") {
        cmd["ball"] = getV(1);
        cmd["std"] = getV(2);
        cmd["sub"] = getV(3);
    }

    if (mt === "C" || mt === "S") {
        cmd["tech"] = getV(0);
        cmd["ball"] = getV(1);
    }

    if (mt === "C") {
        cmd["stuff"] = getV(2);
    }

    if (mt === "S") {
        cmd["face"] = ext_tmp[2];
    }

    return cmd;
};

const aks_d1 = (v) => {
    const cmd = {
        "+": "+0",
        "1": "+1",
        "2": "+2",
        "3": "+3",
        "4": "+4",
        "5": "+5",
        "6": "+6",
        "7": "+7",
        "8": "+8",
        "9": "+9",
        "A": "+10",
        "B": "+11",
        "C": "+12",
        "D": "+13",
        "E": "+14",
        "F": "+15",
        "G": "+16",
        "-": "-0",
        "J": "-1",
        "K": "-2",
        "L": "-3",
        "M": "-4",
        "N": "-5",
        "O": "-6",
        "P": "-7",
        "Q": "-8",
        "R": "-9",
        "S": "-10",
        "T": "-11",
        "U": "-12",
        "V": "-13",
        "W": "-15",
        "X": "-15",
        "Y": "-16",
    };

    const ret = cmd["" + v];
    if (!ret) {
        return "";
    }

    return ret;
};

const qr_snum = (qr, snum) => {
    const tmp = qr.split("#");
    if (tmp.length !== 7) {
        return null;
    }

    tmp[5] = snum;

    return tmp.join("#");
};

const up_qr = (cur, snum) => {
    let qr = obj_str(cur, "qr");
    qr = qr_snum(qr, snum);

    cur["qr"] = qr;
    cur["snum"] = snum;
};

export {up_qr, aks_d1, qr_cmd, qr_snum, load_stuff, print_value, get_side, load_pick, get_tp, load_supply, get_mt};
